package com.ssyc.student.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.SpKeys;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.ScreenUtils;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.R;
import com.ssyc.student.activity.StudentDepotActivity;
import com.ssyc.student.bean.DepotInfo;
import com.ssyc.student.bean.ResultInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Map;

import okhttp3.Call;


/**
 * 学生端--道具--列表适配器
 * Created by Administrator on 2018/4/27 0027.
 */

public class StDepotPropAdapter extends BaseQuickAdapter<DepotInfo.DataBean.Type3Bean, BaseViewHolder> {
    private Context context;
    private Activity activity;
    //屏幕宽度和高度
    private int screenWidth, screenHeight;

    //布局管理参数
    private RelativeLayout.LayoutParams lpOut;
    //条目顶部包含图片的布局参数
    private RelativeLayout.LayoutParams lpTop;

    //背景图片的ImageView的布局参数
    private RelativeLayout.LayoutParams lpLv;

    //顶部New标识的布局参数
    private RelativeLayout.LayoutParams lpNew;
    //显示价格的TextView布局参数
    private RelativeLayout.LayoutParams lpPrice;
    //底部Buy按钮的布局参数
    private RelativeLayout.LayoutParams lpBuy;

    public StDepotPropAdapter(Activity activity, Context context, int layoutResId, @Nullable List<DepotInfo.DataBean.Type3Bean> data) {
        super(layoutResId, data);
        this.context = context;
        this.activity = activity;
        initLaylouPrams(context);
    }

    /**
     * 初始化各控件的布局参数,用于做屏幕的条目宽高适配处理
     *
     * @param context
     */
    private void initLaylouPrams(Context context) {
        screenWidth = ScreenUtils.getScreenWidth(context);
        screenHeight = ScreenUtils.getScreenHeight(context);
        //最外层的布局管理参数
        int width = screenWidth * 222 / 248;
        lpOut = new RelativeLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT);
        lpOut.addRule(RelativeLayout.CENTER_HORIZONTAL);


        //顶部包含布局的布局参数
        int topHeight = width * 215 / 222;
        lpTop = new RelativeLayout.LayoutParams(width, topHeight);
        lpTop.topMargin = width * 2 / 222;

        //显示背景图片的布局参数
        int lvWidth = width * 186 / 222;
        int lvHeight = width * 158 / 222;
        lpLv = new RelativeLayout.LayoutParams(lvWidth, lvHeight);
        lpLv.addRule(RelativeLayout.CENTER_HORIZONTAL);
        lpLv.topMargin = width * 10 / 222;


        //顶部New标识的布局参数
        int newWidth = width * 38 / 222;
        int newHeight = newWidth;
        lpNew = new RelativeLayout.LayoutParams(newWidth, newHeight);
        lpNew.leftMargin = width * 4 / 222;

        //显示价格的TextView布局参数
        lpPrice = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lpPrice.addRule(RelativeLayout.CENTER_HORIZONTAL);
        lpPrice.topMargin = width * 189 / 222;

        //底部Buy按钮的布局参数
        int buyWidth = width * 48 / 222;
        int buyHeiht = buyWidth * 20 / 48;
        lpBuy = new RelativeLayout.LayoutParams(buyWidth, buyHeiht);
        lpBuy.addRule(RelativeLayout.CENTER_HORIZONTAL);
        lpBuy.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lpBuy.bottomMargin = width * 10 / 222;
    }

    @Override
    protected void convert(BaseViewHolder helper, DepotInfo.DataBean.Type3Bean item) {
        RelativeLayout rlOut = helper.getView(R.id.rl_out);
        rlOut.setLayoutParams(lpOut);
        RelativeLayout rlTop = helper.getView(R.id.rl_top);
        rlTop.setLayoutParams(lpTop);
        //图片的布局参数
        ImageView lvProp = helper.getView(R.id.lv_prop);
        lvProp.setLayoutParams(lpLv);
        if (1 == item.getFLAG()) {
            //已购买
            ImageUtil.displayImage(context, lvProp, item.getBLACK());
        } else {
            //未购买
            ImageUtil.displayImage(context, lvProp, item.getCOLOR());
        }

        //顶部显示New标识的图片
        ImageView lvNew = helper.getView(R.id.lv_new);
        lvNew.setLayoutParams(lpNew);

        if (1 == item.getFLAG()) {
            //已购买
            lvNew.setVisibility(View.VISIBLE);
            lvNew.setImageResource(R.drawable.st_clothing_purchashed);
        } else {
            //未购买,金币不够的情况和金币够的情况
            lvNew.setVisibility(View.GONE);
//            lvNew.setImageResource(R.drawable.st_clothing_new);
        }

        //价格
        TextView tvPrice = helper.getView(R.id.tv_price);
        tvPrice.setLayoutParams(lpPrice);
        tvPrice.setText(item.getPRICE() + "");

        //购买按钮
        Button btBuy = helper.getView(R.id.bt_buy);
        btBuy.setLayoutParams(lpBuy);

        //根据状态来设置Buy按钮的背景,已经购买过或者金币不够按钮显示灰色
        if (1 == item.getFLAG()) {
            //已购买
            btBuy.setEnabled(false);
            btBuy.setBackgroundResource(R.drawable.st_bug_purchased);
        } else if (0 == item.getFLAG()) {
            //未购买,在根据用户余额来区别是否按钮是否高亮并且可以购买
            if (((StudentDepotActivity) activity).getTotalMoney() < item.getPRICE()) {
                //用户的总余额<当前商品的价格,则按钮显示为灰色不可点击购买的状态
                btBuy.setEnabled(false);
                btBuy.setBackgroundResource(R.drawable.st_bug_normal);
            } else {
                btBuy.setEnabled(true);
                btBuy.setBackgroundResource(R.drawable.st_bug_select);
            }
        }


        //点击事件
        btBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹出是否购买的弹框
                shopMessagePop(item);
            }
        });
    }


    /**
     * 弹出是否购买的提示框
     *
     * @param item
     */
    private void shopMessagePop(DepotInfo.DataBean.Type3Bean item) {
        PopUpManager.showPop(context, R.layout.student_pop_clothing_and_prop_buy, 0.3f, activity, new PopUpManager.onGetViewListener() {
            @Override
            public void getChildView(View view, int layoutResId) {
                //标题
                TextView tvTile = view.findViewById(R.id.tv_title);
                tvTile.setText("确认购买该道具?");
                //商品图片
                ImageView lvShop = view.findViewById(R.id.lv_shop);

                //商品价格
                TextView tvPrice = view.findViewById(R.id.tv_price);
                tvPrice.setText(item.getPRICE() + "");
                //商品说明
                TextView tvGoods = view.findViewById(R.id.tv_goods);
                tvGoods.setText(item.getCONTENT() + "");
                //关闭图片
                ImageView lvColse = view.findViewById(R.id.lv_close);
                lvColse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUpManager.dismiss();
                    }
                });
                //确定按钮
                Button btBuy = view.findViewById(R.id.bt_buy);
                btBuy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        httpBuy(item);
                    }
                });
            }
        });
    }


    /**
     * 请求服务器进行商品购买
     *
     * @param item 商品
     */
    private void httpBuy(DepotInfo.DataBean.Type3Bean item) {
        String acc = SPUtil.getString(context, SpKeys.ACCOUNT);
        String token = SPUtil.getString(context, SpKeys.TOKEN1);

        Map<String, String> map = HttpUtils.getStudentMap();
        map.put("type", "7");
        map.put("boxitem", item.getROW_ID() + "");//商品ID
        map.put("boxtype", 3 + "");//美食1 服装2 道具3
        map.put("acc", acc);//账号
        map.put("role", "0");//角色
        map.put("apptoken", token);//token
        map.put("platform", "8");

        HttpUtils.post(HttpAdress.DEPOT, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    //解析返回服务器返回的JSON数据
                    ResultInfo info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    if ("200".equals(info.state)) {
                        //购买成功
                        //更改当前条目已经够买的状态
                        item.setFLAG(1);
                        //通过EventBus更改Acitivity的总金币余额
                        EventBus.getDefault().post(new BusInfo(item.getPRICE()));
                        UiUtils.Toast("购买成功", false);
                        PopUpManager.dismiss();
                    } else {
                        //购买失败
                        UiUtils.Toast("购买失败", false);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "解析服务器返回数据异常");
                }

            }
        });
    }
}
